import xlrd
import requests
import json

class APIError(Exception):
    """An API Error Exception"""

    def __init__(self, status):
        self.status = status

    def __str__(self):
        return "APIError: status={}".format(self.status)


loc = ("C:\\Users\\rkommine\\Workspace\\gitlab\\USCreate_JIRA_REST_API\\Tasks.xlsx")


wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
print(1,sheet.nrows)

for i in range(1,sheet.nrows):
    #print(sheet.cell_value(i, 0),sheet.cell_value(i, 1))
    data1 = {
	"fields":{
		"project":{
			"key": "SDLST29",
			"name": "TM_ SDLST29"
		},
		"issuetype":{
			"name": "User Story"     
		},
		"summary": sheet.cell_value(i, 1),
		"reporter":{
			"name": "rkommine",
			"key": "rkommine"
		},
		"components":[{
			"name": "PGW_NeVe"        
		}],
		"description": "",
		"fixVersions": [      {
			"name": sheet.cell_value(i, 4)         
		}],
		"priority":       {
			"name": "Major"
		},
		"customfield_12790": sheet.cell_value(i, 0),
		"timetracking":       {
         		"originalEstimate": sheet.cell_value(i, 5)
		},
                #"customfield_11990": ["com.atlassian.greenhopper.service.sprint.Sprint@3fc44212[id=36624,rapidViewId=11124,state=FUTURE,name="+sheet.cell_value(i, 2)+",startDate=<null>,endDate=<null>,completeDate=<null>,sequence=36624,goal=<null>]"],
                "customfield_11990": int(sheet.cell_value(i, 2)),
                "assignee": {
                    "name": sheet.cell_value(i, 3)                    
                }
	}
    }
    print(data1)
    resp = requests.post('https://jiradc2.int.net.nokia.com/rest/api/2/issue',
                     data=json.dumps(data1),
                     headers={'Content-Type':'application/json','Authorization':'Basic cmtvbW1pbmU6IVdlbGMwbWUk'})
    if resp.status_code != 201:
        #raise APIError('POST /tasks/ {}'.format(resp.status_code))
        print(resp.status_code)
    print('Created task. ID: {}'.format(resp.json()))
                         
                         
    
    
